/*
 *  thread.cpp
 */

#include <pthread.h>
#include "thread.h"
#include "thread_exception.h"
#include "runnable.h"

void* Thread::starter(void* args)
{
    Thread* thread = (Thread*) args;
    (thread->runnable)->run();
    return NULL;	
}

Thread::Thread(Runnable& runnable)
{
    this->runnable = &runnable;
}

Thread::~Thread() {}

void Thread::start() throw(ThreadException)
{
    int retval = pthread_create(&(this->pthread), NULL, starter, this);
    if (retval != 0) {
        ThreadException e;
        throw e;
    }
}

void Thread::join() throw(ThreadException)
{
    int retval = pthread_join(this->pthread, NULL);
    if (retval != 0) {
        ThreadException e;
        throw e;
    }
}

//{+}

